/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.noraxidium;

import java.util.Formatter;

import org.apache.log4j.Logger;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.pleasantnightmare.noraxidium.collision.CollisionRenderer;
import org.pleasantnightmare.noraxidium.info.InfoRenderer;
import org.pleasantnightmare.noraxidium.math.GameVector2f;
import org.pleasantnightmare.noraxidium.model.GameObject;
import org.pleasantnightmare.noraxidium.model.InputController;
import org.pleasantnightmare.noraxidium.model.LevelMap;
import org.pleasantnightmare.noraxidium.model.StaticImage;
import org.pleasantnightmare.noraxidium.support.DisplaySupport;
import org.pleasantnightmare.noraxidium.support.MapSupport;

/**
 * @author deus
 * @version 1.0
 * @since Jul 7, 2010 2:43:01 PM
 */
public class NoraxidiumGame extends BasicGame {
  private static final Logger LOGGER = Logger.getLogger(NoraxidiumGame.class);
  private static final String TITLE = "Noraxidium (v.1.0-SNAPSHOT)";
  private MapSupport mapSupport;
  private GameObject player;

  private CollisionRenderer collisionRenderer;
  private InfoRenderer infoRenderer;

  public NoraxidiumGame() {
    super(TITLE);
  }

  @Override public void init(GameContainer container) throws SlickException {
    NoraxidiumGameContainer ngc = (NoraxidiumGameContainer) container;

    DisplaySupport screenSupport = ngc.getSupport(DisplaySupport.class);
    screenSupport.activateMapLoadingScreen();

    mapSupport = ngc.getSupport(MapSupport.class);
    LevelMap startingMap = mapSupport.loadMap(ngc.getStartingMap());
    mapSupport.activateMap(startingMap);

    Image playerImage = new Image("sprites/ninja.png");
    player = new GameObject(ngc, "player", mapSupport.getPlayerStart(), new StaticImage(playerImage));
    player.addController(new InputController(ngc.getInput()));

    startingMap.addGameObject(player);

    LOGGER.info("Game initialized.");
  }

  @Override public void update(GameContainer container, int delta) throws SlickException {
    // Update input
    NoraxidiumGameContainer ngc = (NoraxidiumGameContainer) container;
    ngc.getInput().poll(ngc.getWidth(), ngc.getHeight());

    // Update level with all objects
    mapSupport.getCurrentMap().update(delta);
  }

  @Override public void render(GameContainer container, Graphics g) throws SlickException {
    NoraxidiumGameContainer ngc = (NoraxidiumGameContainer) container;

    g.clear();
    LevelMap currentMap = mapSupport.getCurrentMap();
    currentMap.render(0, 0, g);

    if (ngc.isDebug())
      renderCollision(g, currentMap);
    
    if (infoRenderer == null)
    	infoRenderer = new InfoRenderer(ngc);
    
    infoRenderer.setCurrentMap(currentMap);
    infoRenderer.setPlayer(player);
    infoRenderer.render(g);
  }

  private void renderCollision(Graphics g, LevelMap currentMap) {
    if (collisionRenderer == null)
      collisionRenderer = new CollisionRenderer();

    collisionRenderer.setLevelMap(currentMap);
    collisionRenderer.render(g);
  }
}
